/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.springboot.jeesite.modules.sys.web;

import com.springboot.jeesite.common.config.Global;
import com.springboot.jeesite.common.persistence.Page;
import com.springboot.jeesite.common.utils.StringUtils;
import com.springboot.jeesite.common.web.BaseController;
import com.springboot.jeesite.modules.sys.entity.Task;
import com.springboot.jeesite.modules.sys.service.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 定时任务配置Controller
 *
 * @author xxx
 * @version 2019-01-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/task")
public class TaskController extends BaseController {

    @Autowired
    private TaskService taskService;

    @ModelAttribute
    public Task get(@RequestParam(required = false) String id) {
        Task entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = taskService.get(id);
        }
        if (entity == null) {
            entity = new Task();
        }
        return entity;
    }

    @RequiresPermissions("sys:task:view")
    @RequestMapping(value = {"list", ""})
    public String list(Task task, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Task> page = taskService.findPage(new Page<Task>(request, response), task);
        model.addAttribute("page", page);
        return "modules/sys/taskList";
    }

    @RequiresPermissions("sys:task:view")
    @RequestMapping(value = "form")
    public String form(Task task, Model model) {
        model.addAttribute("sysTask", task);
        return "modules/sys/taskForm";
    }

    @RequiresPermissions("sys:task:edit")
    @RequestMapping(value = "save")
    public String save(Task task, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, task)) {
            return form(task, model);
        }
        taskService.save(task);
        addMessage(redirectAttributes, "保存定时任务配置成功");
        return "redirect:" + Global.getAdminPath() + "/sys/task/?repage";
    }

    @RequiresPermissions("sys:task:edit")
    @RequestMapping(value = "delete")
    public String delete(Task task, RedirectAttributes redirectAttributes) {
        taskService.delete(task);
        addMessage(redirectAttributes, "删除定时任务配置成功");
        return "redirect:" + Global.getAdminPath() + "/sys/task/?repage";
    }

    @RequiresPermissions("sys:task:edit")
    @RequestMapping(value = "changeStatus")
    public String changeJobStatus(String id, String cmd, RedirectAttributes redirectAttributes) {
        try {
            taskService.changeStatus(id, cmd);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        addMessage(redirectAttributes, cmd + "定时任务配置成功");
        return "redirect:" + Global.getAdminPath() + "/sys/task/?repage";
    }

    @RequiresPermissions("sys:task:edit")
    @RequestMapping(value = "runAJobNow")
    public String runAJobNow(String id, RedirectAttributes redirectAttributes) {
        try {
            taskService.runAJobNow(id);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        addMessage(redirectAttributes, "执行成功");
        return "redirect:" + Global.getAdminPath() + "/sys/task/?repage";
    }
}